package top.glkj.hms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.glkj.hms.entity.Img;
import top.glkj.hms.service.ImgService;
import top.glkj.hms.utils.FileSystem;
import top.glkj.hms.utils.Result;
import top.glkj.hms.utils.ResultFactory;
import top.glkj.hms.utils.StaticValue;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;

/**
 * @author : xgl
 * @version : 0.0.1
 * @date :2020/12/22 2:46
 */
@Api(tags = "图片资源管理")
@Controller
@RequestMapping("/img")
public class ImgController {
    @Value("${file.uploadFolder}")
    private String uploadFolder;
    @Resource
    private ImgService imgService;

    @ApiOperation("获取所有图片文件")
    @RequestMapping("getAllFile")
    public String getAllImg(Model model) {
        model.addAttribute("allImg", FileSystem.getAllFileName(uploadFolder));
        return "img";
    }

    /**
     * @param id
     * @return
     */
    @ApiOperation("根据图片id重定向到文件路由")
    @PermitAll
    @GetMapping("/getImg")
    public String getImgById(Long id) {
        Img img = imgService.getImgById(id);
        return "redirect:/api/img/" + img.getUrl() + "/" + img.getName();
    }

    @ApiOperation("根据图片id获取文件路由")
    @ResponseBody
    @PostMapping("/getImg")
    public Result getImgUrl(Long id) {
        Img img = imgService.getImgById(id);
        return ResultFactory.isSuccess(StaticValue.GET_DATA_SUCCESS, "/api/api/img/" + img.getUrl() + "/" + img.getName());
    }
    @ApiOperation("上传图片")
    @PostMapping
    @ResponseBody
    public Result uploadImg(Img img){return ResultFactory.isError();};
}
